package com.example.demo.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.Bean.Region;
import com.example.demo.Layout.RvSlideLayout;
import com.example.demo.R;
import com.example.demo.Utils.MyHolderView;

import java.util.Collections;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class RegionAdapter extends RecyclerView.Adapter<MyHolderView> {
    private List<Region> mylist;
    private Context context;
    private RvSlideLayout mOpenMenu;
    private RvSlideLayout mScrollingMenu;

    public RegionAdapter(List<Region> mylist, Context context) {
        this.mylist = mylist;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolderView onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.region_item_layout,viewGroup,false);

        return new MyHolderView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolderView myHolderView, @SuppressLint("RecyclerView") final int i) {
        String title = mylist.get(i).getName() + "市";
        if(mylist.get(i).getTop().equals("1")) {
            title = "<font color='#b87adf'>"+mylist.get(i).getName() + "市"+"</font>";
        }
        myHolderView.title.setText(Html.fromHtml(title));
        myHolderView.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String objectId = mylist.get(i).getObjectId();
                Intent intent = new Intent(context, com.example.demo.RegionActivity.class);
                intent.putExtra("name",mylist.get(i).getName());
                context.startActivity(intent);
            }
        });
        myHolderView.title.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Region region = new Region();
                region.setName(mylist.get(i).getName());
                region.setBelong(mylist.get(i).getBelong());
                String tag = mylist.get(i).getTop();
                for(int m = 0; m < mylist.size(); m++) {
                    mylist.get(m).setTop("0");
                    mylist.get(m).update(mylist.get(m).getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                        }
                    });
                }
                if(tag.equals("1")) {
                    region.setTop("0");
                    mylist.get(i).setTop("0");
                    region.update(mylist.get(i).getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e==null){
                                Toast.makeText(context,"取消置顶成功",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    Collections.swap(mylist,i,mylist.size() - 1);
                } else {
                    region.setTop("1");
                    mylist.get(i).setTop("1");
                    region.update(mylist.get(i).getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e==null){
                                Toast.makeText(context,"置顶成功",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    Collections.swap(mylist,0,i);
                }
                notifyDataSetChanged();
                return true;
            }
        });//长按置顶
        myHolderView.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String objectId = mylist.get(i).getObjectId();
                Region mregion = new Region();
                mregion.delete(objectId, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            Toast.makeText(context,"删除成功",Toast.LENGTH_LONG).show();
                        }
                    }
                });
                mylist.remove(i);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mylist.size();
    }

    public void setScrollingMenu(RvSlideLayout scrollingMenu) {
        mScrollingMenu = scrollingMenu;
    }

    public RvSlideLayout getScrollingMenu() {
        return mScrollingMenu;
    }

    public void closeOpenMenu() {
        if (mOpenMenu != null && mOpenMenu.isOpen()) {
            mOpenMenu.closeMenu();
            mOpenMenu = null;
        }
    }

    public void holdOpenMenu(RvSlideLayout rvSlideLayout) {
        mOpenMenu = rvSlideLayout;
    }

    public void setAdd(List<Region> list) {
        mylist.clear();
        this.mylist = list;
        notifyDataSetChanged();
    }

}